#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc_core.h"


u8 nt37701_bufferR[70];
u8 nt37701_bufferG[70];
u8 nt37701_bufferB[70];


void NT37701_Inter_BIST(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{	
	//To selece Gray0 pattern in bist mode
	//CMD page0
	u8 PAGE0[7]={0x06,0xf0,0x55,0xaa,0x52,0x08,0x00};
	u8 cmd1[6]={0x05,0xee,0x87,0x78,0x02,0x40};
	u8 cmd2[9]={0x08,0xef,0x00,0x01,0xff,0xff,0xff,0x1f,0xff};	
	SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);
	delay_ms(2);	
	
	SSD2828_W_Array(SigMode,channel,PAGE0,0);
	SSD2828_W_Array(SigMode,channel,cmd1,0);
	SSD2828_W_Array(SigMode,channel,cmd2,0);
	delay_ms(5);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);   
}

void NT37701_Write_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	  SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFfd);
    SSD2828_W_Array(SigMode,channel,buffer,2);                               

    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写寄存器状态  ：ok  
}
void NT37701_Read_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u16 tmp;
    buffer[1]=0x03;
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);                               
    SSD2828_W_Reg(SigMode,channel,0xC1,buffer[2]); //return package size                                
    SSD2828_W_Reg(SigMode,channel,0xC0,0x0001);
    buffer[0] = 0x01;
    buffer[1] = buffer[3];                        
    SSD2828_W_Array(SigMode,channel,buffer,0);
    delay_ms(5);  
    SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
    SSD2828_W_Cmd(SigMode,channel,0xFF);
    for(i=0;i<buffer[2]-1;i++)
    {
        SSD2828_W_Cmd(SigMode,channel,0xFA);		
        tmp=SPI3_Read_u16_Data(channel);
        buffer[4+i]=tmp>>8;
        buffer[5+i]=tmp;
        delay_ms(5);
        i++;
    }
		buffer[0]=0x66;	
		buffer[1]=0x02;;
		SSD2828_W_Reg(SigMode,channel,0xB7,0x034b);
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 寄存器数据  ：ok 
}

void NT37701_Write_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
    u8 i;
	u8 PAGE2[7]={0x06,0xf0,0x55,0xaa,0x52,0x08,0x02};	
		
	if((buffer[3]&0xFF)==0x00)
	{
		for( i = 0;i<18;i++)   //9个绑点                                                                                  
		{nt37701_bufferR[i]=buffer[i+5];}	    
		for( i = 0;i<18;i++)   //9个绑点                                                                                 
		{nt37701_bufferR[i+18]=buffer[i+5+18];}
		for( i = 0;i<14;i++)   //7个绑点	                                                                                  
		{nt37701_bufferR[i+36]=buffer[i+5+36];}
	}
	else if((buffer[3]&0xFF)==0x42)
	{
		for( i = 0;i<18;i++)   //9个绑点                                                                                  
		{nt37701_bufferG[i]=buffer[i+5];}	    
		for( i = 0;i<18;i++)   //9个绑点                                                                                 
		{nt37701_bufferG[i+18]=buffer[i+5+18];}
		for( i = 0;i<14;i++)   //7个绑点	                                                                                  
		{nt37701_bufferG[i+36]=buffer[i+5+36];}
	}
	else if((buffer[3]&0xFF)==0x81)
	{
		for( i = 0;i<18;i++)   //9个绑点                                                                                  
		{nt37701_bufferB[i]=buffer[i+5];}	    
		for( i = 0;i<18;i++)   //9个绑点                                                                                 
		{nt37701_bufferB[i+18]=buffer[i+5+18];}
		for( i = 0;i<14;i++)   //7个绑点	                                                                                  
		{nt37701_bufferB[i+36]=buffer[i+5+36];}
	}
	SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);
	delay_ms(2);	
	SSD2828_W_Array(SigMode,channel,PAGE2,0);	
	delay_ms(2);
	buffer1[0] = 0x02;        
	buffer1[1] = 0xCC;
	buffer1[2] = 0x10;
	SSD2828_W_Array(SigMode,channel,buffer1,0);	
	delay_ms(1); 		
	buffer1[0] = 0x02; 
	buffer1[1] = 0xBF;
	buffer1[2] = buffer[4]; //gamma_group_sel
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	delay_ms(1); 		
	SSD2828_W_Reg(SigMode,channel,0xBC,19); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,0xB0); //GAMMA RED GROUP1
	for(i=0; i<18; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37701_bufferR[i]);	
	}				 

	SSD2828_W_Reg(SigMode,channel,0xBC,19); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,0xB1); //GAMMA RED GROUP2
	for(i=0; i<18; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37701_bufferR[i+18]);	
	}				

	SSD2828_W_Reg(SigMode,channel,0xBC,15); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,0xB2); //GAMMA RED GROUP3
	for(i=0; i<14; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37701_bufferR[i+36]);	
	}		
	delay_ms(2); 		
	//---------------------------------------------------------------------		
	SSD2828_W_Reg(SigMode,channel,0xBC,19); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,0xB3); //GAMMA Green GROUP1
	for(i=0; i<18; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37701_bufferG[i]);	
	}				 

	SSD2828_W_Reg(SigMode,channel,0xBC,19); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,0xB4); //GAMMA Green GROUP2
	for(i=0; i<18; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37701_bufferG[i+18]);	
	}									 
	SSD2828_W_Reg(SigMode,channel,0xBC,15); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,0xB5); //GAMMA Green GROUP3
	for(i=0; i<14; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37701_bufferG[i+36]);	
	}
	delay_ms(2); 		
	//----------------------------------------------------------------------		
	SSD2828_W_Reg(SigMode,channel,0xBC,19); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,0xB6); //GAMMA Blue GROUP1
	for(i=0; i<18; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37701_bufferB[i]);	
	}				 	
	SSD2828_W_Reg(SigMode,channel,0xBC,19); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,0xB7); //GAMMA Blue GROUP2
	for(i=0; i<18; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37701_bufferB[i+18]);	
	}									 
	delay_ms(2); 
	SSD2828_W_Reg(SigMode,channel,0xBC,15); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,0xB8); //GAMMA Blue GROUP3
	for(i=0; i<14; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37701_bufferB[i+36]);	
	}
	delay_ms(2); 	
	//---------------------------------------------------------		
//	buffer1[0] = 0x02;        
//	buffer1[1] = 0xCC;
//	buffer1[2] = 0x30;
//	SSD2828_W_Array(SigMode,channel,buffer1,0);				
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);				
	delay_ms(2); 
	buffer[0]=0x66;	//NT37701
	buffer[1]=0x08;		
	buffer[4] = Uart_Error_None;
	buffer[5] = 0;
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);            //返回 写寄存器状态  ：ok  
}

void NT37701_Gamma_Update(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
    u8 i;
	u8 PAGE2[7]={0x06,0xf0,0x55,0xaa,0x52,0x08,0x02};	

	SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);
	delay_ms(5);			
	SSD2828_W_Array(SigMode,channel,PAGE2,0);	
	delay_ms(2);	
	buffer1[0] = 0x02; 
	buffer1[1] = 0xBF;
	buffer1[2] = buffer[4]; //gamma_group_sel
	SSD2828_W_Array(SigMode,channel,buffer1,0);		
	buffer1[0] = 0x02;        
	buffer1[1] = 0xCC;
	buffer1[2] = 0x30;
	SSD2828_W_Array(SigMode,channel,buffer1,0);	
	buffer1[0] = 0x02;        
	buffer1[1] = 0xCE;
	buffer1[2] = 0x00;
	SSD2828_W_Array(SigMode,channel,buffer1,0);		
	delay_ms(2);				

	SSD2828_W_Reg(SigMode,channel,0xBC,25); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,0xB0); //GAMMA RED GROUP1
	for(i=0; i<24; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37701_bufferR[i]);	
	}				 

	SSD2828_W_Reg(SigMode,channel,0xBC,25); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,0xB1); //GAMMA RED GROUP2
	for(i=0; i<24; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37701_bufferR[i+24]);	
	}				

	SSD2828_W_Reg(SigMode,channel,0xBC,23); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,0xB2); //GAMMA RED GROUP3
	for(i=0; i<22; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37701_bufferR[i+48]);	
	}		
	delay_ms(2); 		
	//---------------------------------------------------------------------		
	SSD2828_W_Reg(SigMode,channel,0xBC,25); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,0xB3); //GAMMA RED GROUP1
	for(i=0; i<24; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37701_bufferG[i]);	
	}				 

	SSD2828_W_Reg(SigMode,channel,0xBC,25); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,0xB4); //GAMMA RED GROUP2
	for(i=0; i<24; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37701_bufferG[i+24]);	
	}									 
	SSD2828_W_Reg(SigMode,channel,0xBC,23); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,0xB5); //GAMMA RED GROUP3
	for(i=0; i<22; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37701_bufferG[i+48]);	
	}
	delay_ms(2); 		
	//----------------------------------------------------------------------		
	SSD2828_W_Reg(SigMode,channel,0xBC,25); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,0xB6); //GAMMA RED GROUP1
	for(i=0; i<24; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37701_bufferB[i]);	
	}				 	
	SSD2828_W_Reg(SigMode,channel,0xBC,25); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,0xB7); //GAMMA RED GROUP2
	for(i=0; i<24; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37701_bufferB[i+24]);	
	}									 
	SSD2828_W_Reg(SigMode,channel,0xBC,23); 
	SSD2828_W_Cmd(SigMode,channel,0xBF);	
	SSD2828_W_Data(SigMode,channel,0xB8); //GAMMA RED GROUP3
	for(i=0; i<22; i++)
	{
		SSD2828_W_Data(SigMode,channel,nt37701_bufferB[i+48]);	
	}
	delay_ms(2); 	
	//---------------------------------------------------------			
	buffer1[0] = 0x02;        
	buffer1[1] = 0xCE;
	buffer1[2] = 0x01;
	SSD2828_W_Array(SigMode,channel,buffer1,0);		
	delay_ms(10); 		

	buffer1[0] = 0x02;        
	buffer1[1] = 0xCC;
	buffer1[2] = 0x00;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	delay_ms(2); 	
	buffer1[0] = 0x02;        
	buffer1[1] = 0xCE;
	buffer1[2] = 0x00;
	SSD2828_W_Array(SigMode,channel,buffer1,0);				
	delay_ms(2); 		

	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);				
	delay_ms(2); 
	buffer[0]=0x66;	//NT37701
	buffer[1]=0x03;		
	buffer[4] = Uart_Error_None;
	buffer[5] = 0;
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);            //返回 写寄存器状态  ：ok   
}

void NT37701_Read_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
		u8 buffer1[7];
		u16 tmp;
		u8 i,j;
		u8 PAGE2[7]={0x06,0xf0,0x55,0xaa,0x52,0x08,0x02};		
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15);	
		SSD2828_W_Array(SigMode,channel,PAGE2,0); 
		delay_ms(5);	    

		buffer1[0] = 0x02;  	
		buffer1[1] = 0xBF;
		buffer1[2] = buffer[4]; //gamma_group_band
		SSD2828_W_Array(SigMode,channel,buffer1,0);		        
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);  
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0012); //返回个数设置	 18为24个  			
		delay_ms(2);			
		buffer1[0] = 0x02;
		if(buffer[3]==0x00)
			buffer1[1] = 0xB0; //GAMMA RED
		else if(buffer[3]==0x42)
			buffer1[1] = 0xB3; //GAMMA GREEN
		else if(buffer[3]==0x81)
			buffer1[1] = 0xB6; //GAMMA BLUE				
        SSD2828_W_Array(SigMode,channel,buffer1,0);			
	    delay_ms(2);  
		
		SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
		SSD2828_W_Cmd(SigMode,channel,0xFF);
		delay_ms(2);		
		for(i=0;i<18;i++)
	  {
		  SSD2828_W_Cmd(SigMode,channel,0xFA);		
		  tmp=SPI3_Read_u16_Data(channel);
			if(buffer[3]==0x00)
			{				
					nt37701_bufferR[i]=tmp>>8;
					nt37701_bufferR[i+1]=tmp;
			}
			else if(buffer[3]==0x42)
			{
					nt37701_bufferG[i]=tmp>>8;
					nt37701_bufferG[i+1]=tmp;			
			}
			else if(buffer[3]==0x81)
			{
					nt37701_bufferB[i]=tmp>>8;
					nt37701_bufferB[i+1]=tmp;						
			}				
			delay_ms(5);
			i++;			
		}			
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0012); //返回个数设置	    
		delay_ms(2);		
		buffer1[0] = 0x02;        
		if(buffer[3]==0x00)
			buffer1[1] = 0xB1; //GAMMA RED
		else if(buffer[3]==0x42)
			buffer1[1] = 0xB4; //GAMMA GREEN
		else if(buffer[3]==0x81)
			buffer1[1] = 0xB7; //GAMMA BLUE		         
		SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	    delay_ms(2);                
		SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
		SSD2828_W_Cmd(SigMode,channel,0xFF);
		delay_ms(2);
			
		for(i=0;i<18;i++)
		  {
			  SSD2828_W_Cmd(SigMode,channel,0xFA);		
			  tmp=SPI3_Read_u16_Data(channel);
				if(buffer[3]==0x00)
				{					
					nt37701_bufferR[i+18]=tmp>>8;
					nt37701_bufferR[i+19]=tmp;
				}
				else if(buffer[3]==0x42)
				{
					nt37701_bufferG[i+18]=tmp>>8;
					nt37701_bufferG[i+19]=tmp;				
				}
				else if(buffer[3]==0x81)
				{
					nt37701_bufferB[i+18]=tmp>>8;
					nt37701_bufferB[i+19]=tmp;				
				}					
			delay_ms(5);
			  i++;			
		  }			
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0012); //返回个数设置	    
		delay_ms(2);				 
		buffer1[0] = 0x02;        
		if(buffer[3]==0x00)
				buffer1[1] = 0xB2; //GAMMA RED
		else if(buffer[3]==0x42)
				buffer1[1] = 0xB5; //GAMMA GREEN
		else if(buffer[3]==0x81)
				buffer1[1] = 0xB8; //GAMMA BLUE		         
		SSD2828_W_Array(SigMode,channel,buffer1,0);		 
		delay_ms(2);                
		SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
		SSD2828_W_Cmd(SigMode,channel,0xFF);
		delay_ms(2);
			
		for(i=0;i<18;i++)
		  {
			  SSD2828_W_Cmd(SigMode,channel,0xFA);		
			  tmp=SPI3_Read_u16_Data(channel);
				if(buffer[3]==0x00)
				{	
					nt37701_bufferR[i+36]=tmp>>8;
					nt37701_bufferR[i+37]=tmp;
				}
				else if(buffer[3]==0x42)
				{	
					nt37701_bufferG[i+36]=tmp>>8;
					nt37701_bufferG[i+37]=tmp;
				}	
				else if(buffer[3]==0x81)
				{	
					nt37701_bufferB[i+36]=tmp>>8;
					nt37701_bufferB[i+37]=tmp;
				}						
			delay_ms(5);
			  i++;			
		  }		
		for(j=0;j<70;j++)
		{
			if(buffer[3]==0x00)				
				buffer[4+j]=nt37701_bufferR[j];	
			else if(buffer[3]==0x42)	
				buffer[4+j]=nt37701_bufferG[j];	
			else if(buffer[3]==0x81)
				buffer[4+j]=nt37701_bufferB[j];	
		}			
    buffer[0]=0x66;	//NT37701
    buffer[1]=0x09;
		
		delay_ms(15);
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);          //返回 读取的 Gamma数据  ：ok  		
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}

void NT37701_Gamma_OTP_Start(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[10];

	u8 PAGE1[7]={0x06,0xf0,0x55,0xaa,0x52,0x08,0x01};	
		
	u8 OTP_en_PAGE0[8]={0x07,0xED,0x7F,0x00,0x00,0x00,0x00,0x00};
	  u8 OTP_en_PAGE1[8]={0x07,0xED,0x80,0x0F,0x00,0x00,0x00,0x00};
	  u8 OTP_en_PAGE2[9]={0x08,0xED,0x00,0xE0,0x00,0x00,0x00,0x00,0x08};		
	  u8 OTP_en_PAGE3[8]={0x07,0xED,0x00,0x00,0x07,0x00,0x00,0x00};	
	  u8 OTP_en_PAGE4[8]={0x07,0xED,0x00,0x00,0x18,0x00,0x00,0x00};	
	  u8 OTP_en_PAGE5[8]={0x07,0xED,0x00,0x00,0x60,0x00,0x00,0x00};
	  u8 OTP_en_PAGE6[8]={0x07,0xED,0x00,0x00,0x80,0x00,0x00,0x00};		
	  u8 OTP_en_PAGE7[8]={0x07,0xED,0x00,0x00,0x00,0x03,0x00,0x00};		
	  u8 OTP_en_PAGE8[8]={0x07,0xED,0x00,0x00,0x00,0x0C,0x00,0x00};			
		
		u8 OTP_all_PAGE[9]={0x08,0xED,0xFF,0xEF,0xFF,0x0F,0x08,0x00,0x08};
			
	  SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15); 
    buffer1[0] = 0x01;
    buffer1[1] = 0x28;                    
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(10); 
		buffer1[0] = 0x01;
    buffer1[1] = 0x11;  	
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(120); 	
	  SSD2828_W_Array(SigMode,channel,PAGE1,0);//Enable CMD2 Page 1
		delay_ms(5); 
	  buffer1[0] = 0x03;
    buffer1[1] = 0xc9; 
	  buffer1[2] = 0x0c;
    buffer1[3] = 0x11; 	
    SSD2828_W_Array(SigMode,channel,buffer1,0);//VRGH_MTP[7:0] = 0x0F (6.2V)
		delay_ms(20); 
	  buffer1[0] = 0x04;
    buffer1[1] = 0xEB; 
	  buffer1[2] = 0x87;
    buffer1[3] = 0x78; 
	  buffer1[4] = 0x01;		
    SSD2828_W_Array(SigMode,channel,buffer1,0);//Enable Internal MTP Power 
		delay_ms(60); 	
	  buffer1[0] = 0x02;
    buffer1[1] = 0xCA; 
	  buffer1[2] = 0x01;		
    SSD2828_W_Array(SigMode,channel,buffer1,0); //PDSTB = 1
		delay_ms(20); 
		buffer1[0] = 0x03;
    buffer1[1] = 0xCA; 
	  buffer1[2] = 0x01;	
	  buffer1[3] = 0x02;	//PTM_RB[3:0] = 2 (Program Mode)
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(20); 

	
		if(buffer[3]==0x02)  //otp Gamma
		{	 
			  buffer1[0] = 0x02;        
				buffer1[1] = 0xCC;
				buffer1[2] = 0x30;
				SSD2828_W_Array(SigMode,channel,buffer1,0);		
				delay_ms(1); 		
				buffer1[0] = 0x02;        
				buffer1[1] = 0xCE;
				buffer1[2] = 0x01;
				SSD2828_W_Array(SigMode,channel,buffer1,0);		
				delay_ms(5);			
				SSD2828_W_Array(SigMode,channel,OTP_en_PAGE2,0);	
		}
		else if(buffer[3]==0xA0)  //otp ALL
		{
			SSD2828_W_Array(SigMode,channel,OTP_all_PAGE,0);	
		}
		else if(buffer[3]==0x00)  //otp PAGE 0
		{
 			SSD2828_W_Array(SigMode,channel,OTP_en_PAGE0,0);	
		}
		else if(buffer[3]==0x01)  //otp PAGE 1
		{	 
 			SSD2828_W_Array(SigMode,channel,OTP_en_PAGE1,0);	
		}
		else if(buffer[3]==0x03)  //otp PAGE 5
		{
  		SSD2828_W_Array(SigMode,channel,OTP_en_PAGE3,0);	
		}
		else if(buffer[3]==0x04)  //otp PAGE 4
		{		 
			SSD2828_W_Array(SigMode,channel,OTP_en_PAGE4,0);	
		}
		else if(buffer[3]==0x05)  //otp PAGE 5
		{
  		SSD2828_W_Array(SigMode,channel,OTP_en_PAGE5,0);	
		}
		else if(buffer[3]==0x06)  //otp PAGE 6
		{
   		SSD2828_W_Array(SigMode,channel,OTP_en_PAGE6,0);	
		}		
		else if(buffer[3]==0x07)  //otp PAGE 7
		{
   		SSD2828_W_Array(SigMode,channel,OTP_en_PAGE7,0);	
		}		
		else if(buffer[3]==0x08)  //otp PAGE 8
		{
   		SSD2828_W_Array(SigMode,channel,OTP_en_PAGE8,0);	
		}				
		delay_ms(50); 	 
		SSD2828_W_Array(SigMode,channel,PAGE1,0);//Enable CMD2 Page 1
		delay_ms(2);		
    buffer1[0] = 0x04;
    buffer1[1] = 0xEE;
    buffer1[2] = 0xA5;    
	  buffer1[3] = 0x5A;    
	  buffer1[4] = 0x3C;    
		SSD2828_W_Array(SigMode,channel,buffer1,0);			
		delay_ms(5000);//	x must be large than 3s if need program all page
	//	SSD2828_W_Array(SigMode,channel,OTP_en_off,0);		
//		buffer[1] = 0x0E;
		buffer[2] = 0x02;
		buffer[3] = Uart_Error_None;   //返回 OTP Gamma数据  ：OK
		buffer[4] = 0;
		STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);         
}

void ProcessForIc66( USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	switch(buffer[1])
	{
    case 0x01:                                      //写寄存器
         NT37701_Write_Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break;	
    case 0x02:                                      //读寄存器
         NT37701_Read_Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break;	
    case 0x03:                                      //自动gamma写
         NT37701_Gamma_Update(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break;			
    case 0x08:                                      //写Gamma数据    
         NT37701_Write_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break;
    case 0x09:                                      //读Gamma数据
         NT37701_Read_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break; 
		case 0x0D://OTP Gamma
         NT37701_Gamma_OTP_Start(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);  break;

    default:	break;	
	}	
}


